import 'package:xws/helpers/as_t.dart';
import 'package:xws/res/images.dart';

/// 用户基础信息
class UserBasicInfo {
  UserBasicInfo({
    required this.avatarUrl,
    required this.city,
    required this.country,
    required this.createdAt,
    required this.gender,
    required this.id,
    required this.integral,
    required this.latitude,
    required this.level,
    required this.longitude,
    required this.nickName,
    required this.description,
    required this.openid,
    required this.phone,
    required this.province,
    required this.region,
    required this.updatedAt,
    required this.publishCount,
    required this.getCount,
    required this.favoriteCount,
    required this.unreadMessageCount,
  });

  factory UserBasicInfo.fromJson(Map<String, dynamic> jsonRes) {
    final Map<String, dynamic> user = asT<Map<String, dynamic>>(jsonRes['user']);

    String _city = asT<String>(user['city']);
    if (_city.isEmpty) _city = '';

    String _avatarUrl = asT<String>(user['avatarUrl']);
    if (_avatarUrl.isEmpty) _avatarUrl = R.defHeader;

    return UserBasicInfo(
      avatarUrl: _avatarUrl,
      city: _city,
      country: asT<String>(user['country']),
      createdAt: asT<String>(user['createdAt']),
      gender: asT<String>(user['gender']),
      id: asT<String>(user['id']),
      integral: asT<int>(user['integral']),
      latitude: asT<int>(user['latitude']),
      level: asT<int>(user['level']),
      longitude: asT<int>(user['longitude']),
      nickName: asT<String>(user['nickName']),
      description: asT<String>(user['description']),
      openid: asT<String>(user['openid']),
      phone: asT<String>(user['phone']),
      province: asT<String>(user['province']),
      region: asT<String>(user['region']),
      updatedAt: asT<String>(user['updatedAt']),
      publishCount: asT<int>(jsonRes['publishCount']),
      getCount: asT<int>(jsonRes['getCount']),
      favoriteCount: asT<int>(jsonRes['favoriteCount']),
      unreadMessageCount: asT<int>(jsonRes['unreadMessageCount']),
    );
  }

  /// 头像
  final String avatarUrl;

  /// 城市
  late String city;

  /// 国家/地区
  final String country;

  /// 注册时间
  final String createdAt;

  /// 性别
  final String gender;

  /// ID
  final String id;

  /// 积分
  final int integral;

  /// 纬度，范围为 -90~90，负数表示南纬
  final int latitude;

  /// 等级
  final int level;

  /// 经度，范围为 -180~180，负数表示西经
  final int longitude;

  /// 昵称
  final String nickName;

  /// 简介
  final String description;

  /// 微信openid
  final String openid;

  /// 手机号
  final String phone;

  /// 省份
  final String province;

  /// 区
  late String region;

  /// 更新时间
  final String updatedAt;

  /// 发布数量
  late int publishCount;

  /// 获得数量
  final int getCount;

  /// 收藏数量
  late int favoriteCount;

  /// 未读消息数
  late int unreadMessageCount;
}
